<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>轮播图</title>

  <style>
    .div1 {
      position: relative;
      left: 0;
      top: 0;
      height: 100px;
      width: 100px;
      border: 1px red solid;
    }

    .banner {
      position: absolute;
      margin-left: 500px;
      height: 100px;
      width: 100px;
      /* border: 1px palevioletred solid; */
      overflow: hidden;
    }

    ul {
      position: absolute;
      width: 1000px;
      padding: 0;
      margin: 0;
      
    }

    ul>li {
      float: left;
      height: 100px;
      width: 100px;
      background-color: blue;
      /* border: 1px green solid; */
      list-style: none;
    }

    .circle {
      position: absolute;
      left: -10px;
      top: 61px;
    }

    .circle li {
      float: left;
      width: 8px;
      height: 8px;
      border: 2px solid green;
      border-radius: 50%;
      list-style: none;
      margin-right: 2px;
      z-index: 4;
      cursor: pointer;
    }

    .current {
      background-color: black;
    }

    .left,
    .right {
      text-decoration: none;
      position: absolute;
      display: none;
      width: 24px;
      height: 20px;
      top: 50%;
      margin-top: -20px;
      background: rgba(0, 0, 0, .3);
      text-align: center;
      line-height: 20px;
      color: #fff;
      font-family: 'icomoon';
      font-size: 18px;
      border-radius: 0px 9px 9px 0px;

    }

    .left {
      margin-left: 77px;
      border-radius: 9px 0px 0px 9px;
    }
  </style>
</head>


<body>
  <div style="width: 800px;">
    <div class="div1">
    </div>

    <div class="banner">
      <ul class="banner-ul">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
      </ul>

      <!-- 小圆圈 -->
      <ol class="circle">
      </ol>

      <a class="left" href="javascript:"></a>
      <a class="right" href="javascript:"></a>
    </div>

</body>


<script>
  // let a = document.querySelector('.div1')
  // console.log(a.parentElement);

  // animale(a,1000,40);

  window.addEventListener('load', function () {

    const config = { index: 0, timer: null };

    const l = document.querySelector('.left')
    const r = document.querySelector('.right')
    l.innerHTML = '<'
    r.innerHTML = '>'

    const circle = document.querySelector(".circle");
    const ul = document.querySelector('.banner-ul');
    const banner = document.querySelector('.banner');
    banner.addEventListener('mouseenter', function () {
      // alert('111111')
      l.style.display = 'block'
      r.style.display = 'block'
      clearInterval(config.timer);
      config.timer = null
    });

    banner.addEventListener('mouseleave', function () {
      // alert('111111')
      l.style.display = 'none'
      r.style.display = 'none'
      play();
    });

    for (let i = 0;i < ul.children.length;i++) {
      let li = document.createElement('li');
      li.addEventListener('click', function () {
        const circleLi = document.querySelector('.circle')
        for (let i = 0;i < circleLi.children.length;i++) {
          circleLi.children[i].className = '';
        }
        this.className = 'current'
        // console.log(this);
      });

      circle.appendChild(li)
    }
    // 默认选择第一张
    circle.children[0].className = 'current'


    const li = ul.children[0].cloneNode(true);
    // console.log(li);
    ul.appendChild(li)
    //右方向点击事件
    //克隆一张

    //宽度
    const parentWidth = banner.offsetWidth;

    r.addEventListener('click', function () {
      // alert('r')
      //小圆点的动态
      let circleOl = document.querySelector('.current')
      let next = circleOl.nextSibling
      circleOl.className = '';
      if (next) {
        next.className = 'current';
      } else {
        circle.firstElementChild.className = 'current'
      }

      //轮播图的动态
      console.log('---------------------------------', ul.offsetLeft);
      if (config.index == ul.children.length - 1) {
        ul.style.left = 0;
        config.index = 0;
      }
      config.index++;
      console.log('动画---------------------------------', -config.index * parentWidth);
      animale(ul, -config.index * parentWidth, 30)
    });

    //左边点击事件
    l.addEventListener('click', function () {
      // alert('l')
      let circleOl = document.querySelector('.current')
      let prev = circleOl.previousElementSibling;
      circleOl.className = '';
      if (prev) {
        prev.className = 'current';
      } else {
        circle.lastElementChild.className = 'current'
      }


      //轮播图的动态
      console.log('---------------------------------', ul.offsetLeft);
      if (config.index == 0) {
        config.index = ul.children.length - 1;
        ul.style.left = -config.index * parentWidth + 'px';

      }
      config.index--;
      console.log('动画---------------------------------', -config.index * parentWidth);
      animale(ul, -config.index * parentWidth, 30)


    });
    // const circleLi = document.querySelector('.' + circle_class_name)

    // circle.children[2].className = 'current'
    play();
    function animale(obj, target = 0, time = 0, callbak) {
      clearInterval(obj.timer)
      obj.timer = setInterval(function () {
        let left = obj.offsetLeft;
        let step = (target - left) / 10
        // console.log(target,left,step);
        step = step > 0 ? Math.ceil(step) : Math.floor(step)

        if (left == target) {
          clearInterval(obj.timer)
        }
        // console.log(target);
        obj.style.left = (left + step) + 'px'
        // console.log(obj.offsetLeft);
      }, time)
    }


    function play() {
      config.timer = setInterval(function () {
        r.click();
      }, 2000)
    }



  })


</script>

</html>